package com.example.utils.impl;

import com.example.entity.res.FileMsg;
import com.example.utils.AlbumUtil;
import com.example.utils.DealNumber;
import com.example.utils.PicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AlbumUtilImpl implements AlbumUtil {

    private final static String STORGE_PATH = "C:\\Users\\Ten.X\\Desktop\\AlbumImg\\";
//
    private final static String STORGE_PATH_COMPRESS = "C:\\Users\\Ten.X\\Desktop\\AlbumImg\\Compress\\";


//    private final static String STORGE_PATH = "/usr/AlbumImg/";
//
//    private final static String STORGE_PATH_COMPRESS = "/usr/AlbumImg/Compress/";

    @Override
    public String downloadFile(String fileName, HttpServletResponse response) {
        if (fileName != null) {
            //设置文件路径
            File file = new File(STORGE_PATH + fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

    @Override
    public String downloadFileCompress(String fileName, HttpServletResponse response) {
        if (fileName != null) {
            //设置文件路径
            File file = new File(STORGE_PATH_COMPRESS + fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return "下载失败";
    }

    public List<FileMsg> uploadFilesmore(List<MultipartFile> files) {
        List<FileMsg> finalmsg = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                FileMsg fileMsg = new FileMsg();
                if (file.isEmpty()) {
                    fileMsg.setPathName("");
                    fileMsg.setSuffixName("");
                    finalmsg.add(fileMsg);
                    return finalmsg;
                }
                //获取文件名
                String fileName = file.getOriginalFilename();
                log.info("上传的文件名：" + fileName);
                //获取文件后缀名
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                log.info("文件后缀名：" + suffixName);
                String myname = DealNumber.imgcode() + suffixName;
                //设置文件存储路径
                String filePath = STORGE_PATH;
                String path = filePath + myname;
                File dest = new File(path);
                //检测是否存在该目录
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }

                String compressName = DealNumber.imgcode() + "compress" + suffixName;

                String compressPath = STORGE_PATH_COMPRESS + compressName;

                DataOutputStream out = new DataOutputStream(new FileOutputStream(compressPath));
                byte[] imageCom = PicUtil.getImageCom(file);

                //写入文件
                out.write(imageCom);

                //写入文件
                file.transferTo(dest);


                fileMsg.setPathName(myname);
                fileMsg.setPathNameCompress(compressName);
                fileMsg.setSuffixName(suffixName);
                finalmsg.add(fileMsg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return finalmsg;
    }

}
